package com.zxn.dragdemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.zxn.dragdemo.adapter.CategorySettingAdapter;
import com.zxn.dragdemo.bean.CategoryInfo;

import java.util.ArrayList;


/**
 * Created by zxn on 2019-9-4 15:47:00.
 */
public class CategorySettingActivity extends AppCompatActivity /*implements  BaseQuickAdapter.OnItemChildClickListener */ {

    private static final String ARG_PARAM1 = "param1";
    protected RecyclerView rvCommon;
    //    @BindView(R.id.tv_add_category)
//    TextView tvAddCategory;
//    @BindView(R.id.tv_save_category)
//    TextView tvSaveCategory;
//    @BindView(R.id.et_add_category)
//    EditText etAddCategory;
//    @BindView(R.id.rl_save_category)
//    RelativeLayout rlSaveCategory;
//    @BindView(R.id.rv_common)
//    RecyclerView rvCommon;
//    @BindView(R.id.tv_category_order)
//    TextView tvCategoryOrder;
    private String mParam1;
    private CategorySettingAdapter mAdapter;
    private ItemTouchHelper mItemTouchHelper;

    public static void jumpTo(Context context, String param1) {
        Intent intent = new Intent(context, CategorySettingActivity.class);
        intent.putExtra(ARG_PARAM1, param1);
        context.startActivity(intent);
    }

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, CategorySettingActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mParam1 = getIntent().getStringExtra(ARG_PARAM1);
        super.setContentView(R.layout.activity_category_setting);
        initView();
        rvCommon.setLayoutManager(new GridLayoutManager(this,3));
        rvCommon.setHasFixedSize(true);
//        rvCommon.addItemDecoration(new RvItemDecoration.Builder(this)
//                .bgColor(R.color.c_f9f9f9)
//                .widthDp(2f)
//                .setOrientation(LinearDecoration.VERTICAL)
//                .createLinear());
        //rvCommon.addItemDecoration(new SpacesItemDecoration(80, 50));


        OnItemDragListener listener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
                //LogUtils.i("drag start");
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
                //LogUtils.d("move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
                //LogUtils.d("drag end");
            }
        };
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(20);
        paint.setColor(Color.BLACK);
        OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
                //LogUtils.d("view swiped start: " + pos);
            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
                //LogUtils.d("View reset: " + pos);
            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                //LogUtils.d("View Swiped: " + pos);
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
                canvas.drawColor(ContextCompat.getColor(CategorySettingActivity.this, com.zxn.drag.R.color.color_light_blue));
            }
        };

        ArrayList<CategoryInfo> data = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            CategoryInfo info = new CategoryInfo();
            info.categoryName = "CategoryInfo--" + i;
            data.add(info);
        }
        mAdapter = new CategorySettingAdapter(data);
        //mAdapter.setOnItemChildClickListener(this);

        ItemDragAndSwipeCallback mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
        mItemTouchHelper.attachToRecyclerView(rvCommon);

        mItemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        mAdapter.disableSwipeItem();
        mAdapter.setOnItemSwipeListener(onItemSwipeListener);
        mAdapter.setOnItemDragListener(listener);


        rvCommon.setAdapter(mAdapter);


        mAdapter.enableDragItem(mItemTouchHelper);
    }



    private void initView() {
        rvCommon = (RecyclerView) findViewById(R.id.rv_common);
    }
}
